package com.zlcx.tz.live.repository;

import com.zlcx.tz.live.domain.LiveRoomInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

/**
 * <p></p>
 * <PRE>
 * <BR>    修改记录
 * <BR>-----------------------------------------------
 * <BR>    修改日期         修改人          修改内容
 * </PRE>
 *
 * @author zl
 * @version 1.0
 * @date Created in 2019/4/18 10:11
 * @copyright: Copyright (c) founders
 */
@Repository
public interface LiveRoomInfoRepository extends JpaRepository<LiveRoomInfo, Long>, JpaSpecificationExecutor<LiveRoomInfo> {


    LiveRoomInfo findFirstByIdAndRoomStateIn(Long id, List<Integer> roomState);

    LiveRoomInfo findFirstByRoomStateInOrderByCreateTimeDescRoomStateAsc(List<Integer> RoomStates);

    @Query(value = "select * from live_room_info p where p.room_state in ?1 ORDER BY p.create_time desc, p.room_state ASC limit 10", nativeQuery = true)
    List<LiveRoomInfo> findByRoomStateInOrderByCreateTimeDescRoomStateAsc(List<Integer> RoomStates);

    @Transactional(rollbackOn = Exception.class)
    @Modifying(clearAutomatically = true)
    @Query("UPDATE LiveRoomInfo lri SET lri.roomState =:roomState , lri.updateTime =:updateTime WHERE lri.id =:roomId ")
    int updateRoomState(@Param("roomState") Integer roomState, @Param("updateTime") Date updateTime, @Param("roomId") Long roomId);


    @Transactional(rollbackOn = Exception.class)
    @Modifying(clearAutomatically = true)
    @Query("UPDATE LiveRoomInfo lri SET lri.roomMicState =:roomMicState , lri.updateTime=:updateTime WHERE lri.id =:roomId ")
    int updateRoomMicState(@Param("roomMicState") Integer roomMicState, @Param("updateTime") Date updateTime, @Param("roomId") Long roomId);

    @Transactional(rollbackOn = Exception.class)
    @Modifying(clearAutomatically = true)
    @Query("UPDATE LiveRoomInfo lri SET lri.roomAvatar =:roomAvatar ,lri.roomDesc =:roomDesc, lri.updateTime=:updateTime WHERE lri.id =:roomId ")
    int updateRoomAvatarAndDesc(@Param("roomAvatar") String roomAvatar, @Param("roomDesc") String roomDesc,@Param("updateTime") Date updateTime, @Param("roomId") Long roomId);


    @Transactional(rollbackOn = Exception.class)
    @Modifying(clearAutomatically = true)
    @Query("UPDATE LiveRoomInfo lri SET lri.roomAvatar =:roomAvatar , lri.updateTime=:updateTime WHERE lri.id =:roomId ")
    int updateRoomAvatar(@Param("roomAvatar") String roomAvatar, @Param("updateTime") Date updateTime, @Param("roomId") Long roomId);

    @Query(value = "select lri.* from live_room_info lri " +
            "where lri.room_state in(1,2) and lri.id not in( " +
            "    select lrjr.room_id from live_room_join_record lrjr " +
            "    where lrjr.join_state = 1 " +
            "    union (" +
            "        select lrpr.room_id from live_room_publisher_record lrpr " +
            "        where lrpr.publisher_state in (1,2) " +
            "    )" +
            ")", nativeQuery = true)
    List<LiveRoomInfo> getUnnormalClosedRoom();


    @Transactional(rollbackOn = Exception.class)
    @Modifying(clearAutomatically = true)
    @Query("UPDATE LiveRoomInfo lri SET lri.roomState =3 , lri.updateTime =:updateTime WHERE lri.id in(:roomIds)  ")
    int closeUnnormalClosedRoom( @Param("roomIds") List<Long> roomIds,@Param("updateTime") Date updateTime);

    @Query(value = "select t.id from live_room_info t " +
            "where t.room_state <= 2", nativeQuery = true)
    List<Long> findAllOpenRoomIds();
}
